//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')

let mkScrenn = require('components/modules/screen')
let mkDropdownList = require('components/modules/dropdownlist')
let mkModal = require('components/modules/modal')

const demo4Data = [
  1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
]
const checkboxData = [{title: '200m以内的', id: 'a1'}, {title: '500m以内的', id: 'b1'}, {title: '1km以内的', id: 'c1'}]

const xx0 = mkDropdownList({
  $$id: 'dd-list-radio-0',
  data: [{title: '从远到近', id: 'a1'}, {title: '从近到远', id: 'b1'}, {title: '附近', id: 'c1'}],
  parentId: '0',
  type: 'radio',
})
const xx1 = mkDropdownList({
  $$id: 'dd-list-checkbox',
  // data: [{title: '200m以内的', id: 'a1'}, {title: '500m以内的', id: 'b1'}, {title: '1km以内的', id: 'c1'}],
  parentId: '1',
  type: 'checkbox',
})
// const xx2 = mkDropdownList({
//   $$id: 'dd-list-radio-1',
//   data: [{title: '深圳', id: 'a11'}, {title: '广州', id: 'b11'}, {title: '上海', id: 'c11'}],
//   parentId: '2',
//   type: 'radio'
// })
// const xx3 = mkDropdownList({
//   $$id: 'dd-list-radio-2',
//   data: [{title: '便宜', id: 'a21'}, {title: '舒适', id: 'b21'}, {title: '品质', id: 'c21'}],
//   parentId: '3',
//   type: 'radio'
// })

Pager({
  data: {
    pages: mkScrenn({
      $$id: 'dd-screen',
      menus: [{title: '位置', type: 'radio', id: '0'}, {title: '车程距离', type: 'checkbox', id: '1'}, {title: '热门城市', type: 'link', id: '2'}, {title: '自驾主题', type: 'other', id: '3'}],
      contents: [
        xx0, xx1, null, null
      ],
      itemClass: 'dropdown-item-content',
      itemWidth: '25%'
      // mask: false
    }),
    demo4: {
      $$id: 'demo4',
      data: demo4Adapater(demo4Data.slice(0,5)),
      itemClass: 'mt-default',
      listClass: 'plr-default',
      footer: {
        $$id: 'loading',
        title: '正在加载中，请稍候...',
        itemClass: 'ss-center color-dot size-sm mt-default'
      }
    },
    modal: { id: 'actionSide1' },
  },
  $screenInst: null,
  isUpdated: false,
  onAppend(e, params, inst) {
    //重置按钮
    console.log(this.$screenInst, xx3)
  },
  // onItem(e, params, inst) {
  //   const type = params.type
  //   const id = params.id
  //   inst.addClass('active')
  //   if (type === 'radio') {
  //     inst.siblings().removeClass('active')
  //   }
  // },
  
  onShow: function () {},
  
  onReady: function () {
    const that = this
    this.$screenInst = this.getElementsById('dd-screen')
    this.modal = this.getElementsById('actionSide1')
    const $radioInst0 = this.getElementsById('dd-list-radio-0')
    this.$listInst = this.getElementsById('demo4')
    // const $radioInst1 = this.getElementsById('dd-list-radio-1')
    // const $radioInst2 = this.getElementsById('dd-list-radio-2')
    const $checkInst = this.getElementsById('dd-list-checkbox')
    $radioInst0.hooks.on('dropdownlist-radio', function(params) {
      const _vals = params.vals
      const _parentId = params.parentId
      that.$screenInst.onClose({parentId: _parentId, vals: _vals})
    })

    this.$screenInst.hooks.on('updateContent', function (params) {
      console.log(params, 'params')
      if (params.type === 'link') {
        that.$listInst.update(demo4Adapater(demo4Data.slice(5,10)))
      }
      if (params.type === 'checkbox' && !that.isUpdated) {
        //异步加载弹出层内容
        $checkInst.updatex(checkboxData)
        that.isUpdated = true
      }
      if (params.type === 'other') {
        that.onModal()
      }
      // const parentId = parseInt(params.id)
      // that.$Inst[params.id].updatex(data[parentId])
    })
    $checkInst.hooks.on('keep-val', function(params) {
      const _vals = params.vals
      const _parentId = params.parentId
      that.$screenInst.onClose({parentId: _parentId, vals: _vals})
    })
    // $radioInst1.hooks.on('dropdownlist-radio', function(params) {
    //   const _vals = params.vals
    //   const _parentId = params.parentId
    //   that.$screenInst.onClose({parentId: _parentId, vals: _vals})
    // })
    // $radioInst2.hooks.on('dropdownlist-radio', function(params) {
    //   const _vals = params.vals
    //   const _parentId = params.parentId
    //   that.$screenInst.onClose({parentId: _parentId, vals: _vals})
    // })

  },
  onLoad: function () {},
  onModal(e, params, inst) {
    this.modal.update(mkModal({
      data: {
        title: [
          {
            title: '标题',
            itemClass: 'title-primary no-lh ss-center mb-default'
          },
          {title: ' ', itemClass: 'icon-close click-icon-width', aim: 'onClose'}
        ],
        body: [{title: '开启微信用户信息授权，即可享受更多服务哦~', itemClass: 'size no-lh-x'}],
        footer: [ {title: '下一步', itemClass: 'ss-button btn-default'}],
        footerClass: 'btn-footer mt50',
        titleClass: 'flex-row-between',
      },
      direction: 'right',
      full: false,
    }))
  },
})

function demo4Adapater (data) {
  return data.map((item, ii) => {
    return {
      body: [
        {title: '2022_'+item+'期', itemClass: 'title-primary no-lh mb-minor'},
        {title: '中奖奖品', itemClass: 'size-sm fw-bold color-fail no-lh mb5'},
        {title: '澳门新濠天地-摩珀斯 豪华套房一澳门新濠天地-摩珀斯 豪华套房一...', itemClass: 'size-sm fw-bold color-title textellipsis-1'},
        {title: '兑奖有效期: 2021年1月11日-2021年2月11日', itemClass: 'size20 color-info no-lh mt15'},
      ],
      footer: [
        {title: '去兑奖', itemClass: 'ss-button btn-default small'}
      ],
      bodyClass: 'item-body',
      footerClass: 'item-footer',
      itemClass: 'coupon-item'
    }
  })
}
